<?php

class Application_Model_SellerMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Seller');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Seller $seller)
    {
        $data = array(
            'emso' => $seller->getEmso(),
            'ime'   => $seller->getIme(),
            'priimek' => $seller->getPriimek(),
            'geslo' => $seller->getGeslo(),
        );

        try {
            $seller = new Application_Model_Seller();
            $this->find($data['emso'], $seller);
            if ($seller->getEmso()) {
                $where = $this->getDbTable()->getAdapter()->quoteInto('emso = ?', $data['emso']);
                $this->getDbTable()->update($data, $where);
            } else {
                $this->getDbTable()->insert($data);
            }
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
            die;
        }

    }

    public function find($id, Application_Model_Seller &$seller)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $seller->setEmso($row->emso)
            ->setIme($row->ime)
            ->setPriimek($row->priimek)
            ->setAktiviran($row->aktiviran)
            ->setGeslo($row->geslo);
    }

    public function delete($id)
    {
        $where = $this->getDbTable()->getAdapter()->quoteInto('emso = ?', $id);
        return $this->getDbTable()->delete($where);
    }

    public function fetchAll($where) {
        return $this->getDbTable()->fetchAll($where);
    }

    public function getAllSellers($where = null) {
        $sellerArray = $this->fetchAll($where);
        $result = array();
        foreach ($sellerArray as $seller) {
            $sellerModel = new Application_Model_Seller();
            $sellerModel->setEmso($seller['emso']);
            $sellerModel->setIme($seller['ime']);
            $sellerModel->setPriimek($seller['priimek']);
            $sellerModel->setAktiviran($seller['aktiviran']);
            $result[$seller['emso']] = $sellerModel;
        }
        return $result;
    }

    public function authenticate($emso, $geslo) {
        $seller = new Application_Model_Seller();

        $this->find($emso, $seller);

        if (password_verify($geslo, $seller->getGeslo()) && $seller->getAktiviran()) {
            return $seller;
        } else {
            return null;
        }
    }

    public function update($emso, $polje, $vrednost) {
        $where = $this->getDbTable()->getAdapter()->quoteInto('emso = ?', $emso);
        $this->getDbTable()->update(array($polje => $vrednost), $where);
    }

}

